import Dao from "../../utils/dao";
import {getLocal} from "../../utils/local";


import {mergerImage} from "./utils";
import {getObject, getQuery, object_w} from "../../utils/databaseUtil";
import {dao_save} from "../../utils/dao/save";
import {MyMap} from "../../utils/lodash";

const product_images = [
    "http://lc-hsgiqTbn.cn-n1.lcfile.com/f2900a789ddb4cc8c5b2.png",
    "http://lc-hsgiqTbn.cn-n1.lcfile.com/dd174a2873b91013e315.png",

    "http://lc-hsgiqTbn.cn-n1.lcfile.com/ebb1ced21792eb8baeee.png",
    "http://lc-hsgiqTbn.cn-n1.lcfile.com/9c39749bccf169526602.png",

    "http://lc-hsgiqTbn.cn-n1.lcfile.com/e1ec083b1e7ea98d4b55.png",
    "http://lc-hsgiqTbn.cn-n1.lcfile.com/5656e257939d869af9b1.png",

    "http://lc-hsgiqTbn.cn-n1.lcfile.com/8a02f8dfae57fb34466a.png",
    "http://lc-hsgiqTbn.cn-n1.lcfile.com/2d4840b862a3e87f6270.png",
]


export async function create_productImages(designId) {
    const finalImages = []
    var design_q = new AV.Query('design');
    design_q.equalTo('objectId', designId)
    var design_result = (await design_q.include("image").find())[0].toJSON()

    for (var i = 0; i < product_images.length; i++) {
        /*上传图片  把图片指针留在数组 finalImages*/
        var product_image = product_images[i]
        var finalImageItem = await  mergerImage(product_image, design_result.image.url)
        var file_av = new AV.File(i + "", {base64: finalImageItem})
        var file_save_result = (await file_av.save()).toJSON()
        finalImages.push(file_save_result.objectId)
    }

    var productImages_av = new (AV.Object.extend('productImages'))()
    var p_design = AV.Object.createWithoutData('design', designId)

    productImages_av.set('design', p_design)
    /*把文件指针 关联到 files*/
    productImages_av.set('files', finalImages)
    var insert_result = await productImages_av.save()
    return insert_result
}


export async function dao_save_sale_count(classname, options) {
    var o = getObject(classname)
    var q = getQuery(classname)
    var one = await q.equalTo('design', object_w('design', options.designId)).first().then(res => res.toJSON()).catch(e => undefined)
    options.design = object_w('design', options.designId)
    if (one) {
        //save
        o.id = options.objectId
        MyMap(options, (value, key) => {
            if (key != 'objectId') {
                o.set(key, value)
            }
        })
    } else {
        //add
        MyMap(options, (value, key) => {
            if (key != 'objectId') {
                o.set(key, value)
            }
        })
    }
    return await  o.save().then(res => res.toJSON())
}

export async function addDesign(_design) {
    var design = _design
    design.user = AV.Object.createWithoutData('user', getLocal("app.login_user").objectId)
    design.image = AV.File.createWithoutData(design.image.objectId)
    // AV.File.withURL(design.image.objectId, design.image.url)
    design.status = "4"
    var designDao = new Dao("design")
    var insertResult = (await designDao.insert(design)).toJSON()
    await create_productImages(insertResult.objectId)

    //sale count 中,每个产品的cont 都是0
    var sale_count_insert = await  dao_save_sale_count('salesCount', {designId: insertResult.objectId, count: 0})
    return insertResult
}

